package com.link.play.fit.ai.ui.guide

import android.os.Bundle
import android.view.View
import com.aigestudio.wheelpicker.WheelPicker.OnWheelChangeListener
import com.link.play.fit.ai.R
import com.link.play.fit.ai.bean.UserGuide
import com.link.play.fit.ai.databinding.FgBornBinding
import com.link.play.fit.ai.ui.guide.ac.GuidePbAc
import com.link.play.fit.ai.ui.setting.SetUserInfoAc
import com.link.play.fit.ai.utils.DateEx.daysInMonth
import com.link.play.fit.ai.utils.DateEx.getYearFromTimestamp


class BornFg : BasePageFragment<FgBornBinding>(FgBornBinding::inflate) {


    private val yearList by lazy {
        val list = mutableListOf<String>()
        var start = 1900 - 1
        repeat(getYearFromTimestamp(System.currentTimeMillis()) - start) {
            start++
            list.add("$start")
        }
        list.toList()
    }
    private val monthList by lazy {
        listOf(
            getString(R.string.january),
            getString(R.string.february),
            getString(R.string.march),
            getString(R.string.april),
            getString(R.string.may),
            getString(R.string.june),
            getString(R.string.july),
            getString(R.string.august),
            getString(R.string.september),
            getString(R.string.october),
            getString(R.string.november),
            getString(R.string.december),
        )
    }

    lateinit var userGuideState: UserGuide
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        when (val ac = requireActivity()) {
            is GuidePbAc -> {// 引导页面
                userGuideState = ac.viewModel.userGuideState.value
            }

            is SetUserInfoAc -> {//设置界面的要用
                userGuideState = ac.viewModel.userGuideState.value
                binding.tvTitle.visibility = View.GONE
                binding.tvContent.visibility = View.GONE
            }
        }

        binding.wheelYear.data = yearList
        binding.wheelMonth.data = monthList
        binding.wheelDay.data = getDaysList(userGuideState.bornYear, userGuideState.bornMonth)

        binding.wheelYear.setSelectedItemPosition(
            yearList.indexOf("${userGuideState.bornYear}"),
            false
        )
        binding.wheelMonth.setSelectedItemPosition(userGuideState.bornMonth - 1, false)
        binding.wheelDay.setSelectedItemPosition(userGuideState.bornDay - 1, false)

        binding.wheelYear.setOnWheelChangeListener(object : OnWheelChangeListener {
            override fun onWheelScrolled(offset: Int) = Unit
            override fun onWheelScrollStateChanged(state: Int) = Unit
            override fun onWheelSelected(position: Int) {
                userGuideState.bornYear = yearList[position].toInt()
                binding.wheelDay.data =
                    getDaysList(userGuideState.bornYear, userGuideState.bornMonth)

            }
        })
        binding.wheelMonth.setOnWheelChangeListener(object : OnWheelChangeListener {
            override fun onWheelScrolled(offset: Int) = Unit
            override fun onWheelScrollStateChanged(state: Int) = Unit
            override fun onWheelSelected(position: Int) {
                userGuideState.bornMonth = position + 1
                binding.wheelDay.data =
                    getDaysList(userGuideState.bornYear, userGuideState.bornMonth)
            }
        })
        binding.wheelDay.setOnWheelChangeListener(object : OnWheelChangeListener {
            override fun onWheelScrolled(offset: Int) = Unit
            override fun onWheelScrollStateChanged(state: Int) = Unit
            override fun onWheelSelected(position: Int) {
                userGuideState.bornDay = position + 1
            }
        })

    }


    private fun getDaysList(year: Int, month: Int): List<String> {

        val daysInMonth = daysInMonth(year, month)

        val list = mutableListOf<String>()
        var start = 1 - 1
        repeat(daysInMonth - start) {
            start++
            if (start < 10) {
                list.add("0$start")
            } else {
                list.add("$start")
            }
        }
        return list.toList()
    }

}